/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.ui.util;

import java.util.List;

import javax.swing.table.AbstractTableModel;

import org.apache.log4j.Logger;

/**
 * @author stl
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class ColorPatternLegendTableModel extends AbstractTableModel {
		
    private static Logger logger = Logger.getLogger(ColorPatternLegendTableModel.class); 

    private static final int RANGE_ID_COL =0;
	private static final int LEGEND_COL =1;
	
	private static final String[] columnNames = { "Range #", "Color/Pattern legend per range" };
	
	private List<String> colorPatternLegend;	
	private boolean dirty =false;	
	
	public boolean isDirty() {
		return dirty;
	}

	public void setDirty(boolean dirty) {
		this.dirty = dirty;
	}
	
	public void setColorPatternLegend( List<String> colorPatternLegend ) {
		this.colorPatternLegend =colorPatternLegend;
		
		setDirty( false );
		
        fireTableDataChanged();
	}
	
	public List<String> getColorPatternLegend() {
		return colorPatternLegend;
	}
										  
	public int getColumnCount() {
		return columnNames.length;
	}

	public int getRowCount() {
		return colorPatternLegend !=null ? colorPatternLegend.size() : 0;
	}

	public String getColumnName(int col) {
		return columnNames[col];
	}

	public Object getValueAt(int row, int col) {
		Object obj =null;
		
		switch( col ) {
			case RANGE_ID_COL:
				obj =Integer.valueOf( row + 1 );
				break;
		
			case LEGEND_COL:
				obj =colorPatternLegend.get( row );
				break;
		}
		
		return obj;
	}
	
    public void setValueAt(Object value, int row, int col) {
		
		switch( col ) {
			case LEGEND_COL:
				colorPatternLegend.set( row, (String)value );
				break;
		}
    	                    
        fireTableCellUpdated(row, col);
        
        setDirty( true );
    }	

	/*
	 * JTable uses this method to determine the default renderer/
	 * editor for each cell.  If we didn't implement this method,
	 * then the last column would contain text ("true"/"false"),
	 * rather than a check box.
	 */
	@SuppressWarnings("unchecked")
	public Class getColumnClass(int c) {
		return getValueAt(0, c).getClass();
	}

	/*
	 * Don't need to implement this method unless your table's
	 * editable.
	 */
	public boolean isCellEditable(int row, int col) {
		return ( col == LEGEND_COL ); 
	}

     
}
